package com.drug.lib.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.drug.lib.R;


public class EmptyLayout extends LinearLayout {

    public static final int SHOW_CONTENT_VIEW = 0;//正常显示加载页面
    public static final int NETWORK_ERROR = 1;//无网络
    public static final int NETWORK_LOADING = 2;//网络加载中
    public static final int NODATA = 3;//无数据
    public static final int LOAD_FAILD = 4;//网络加载失败
    public static final int LOAD_SUCCESS = 5;//网络加载完成

    private ProgressBar mLoading;
    public ImageView img;
    private TextView tv;
    private Button retryBtn;

    private int mErrorState = SHOW_CONTENT_VIEW;

    public EmptyLayout(Context context) {
        super(context);
        init();
    }

    public EmptyLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.view_empty_layout, this, false);
        img = (ImageView) view.findViewById(R.id.img_error_layout);
        tv = (TextView) view.findViewById(R.id.tv_error_layout);
        retryBtn = view.findViewById(R.id.btn_Retry);
        mLoading = (ProgressBar) view.findViewById(R.id.animProgress);
        mLoading.setVisibility(GONE);
        retryBtn.setOnClickListener(v -> {
            if (retryButtonClick != null)
                retryButtonClick.onClickFromRetry();
        });
        addView(view);
    }


    public void dismiss() {
        setVisibility(View.GONE);
    }

    public int getErrorState() {
        return mErrorState;
    }

    /**
     * description: 设置错误信息
     *
     * @param msg
     * @author: zhouhequan
     * @date: 2022/7/1 23:45
     * @return: void
     */

    public void setErrorMessage(String msg) {
        if (TextUtils.isEmpty(msg)) {
            tv.setText(R.string.error_view_no_data);
        } else {
            tv.setText(msg);
        }
    }

    /**
     * description: 设置按钮文本
     *
     * @param msg
     * @author: zhouhequan
     * @date: 2022/7/1 23:44
     * @return: void
     */

    public void setRetryMessage(String msg) {
        retryBtn.setText(msg);
    }


    /**
     * 新添设置背景
     */
    public void setErrorImag(int imgResource) {
        img.setImageResource(imgResource);
    }

    public void setErrorType(int status) {
        switch (status) {
            case NETWORK_ERROR:
                mErrorState = NETWORK_ERROR;
                tv.setText(R.string.tip_network_error);
                img.setImageResource(R.mipmap.net_erro);
                retryBtn.setText(R.string.reload);

                retryBtn.setVisibility(VISIBLE);
                img.setVisibility(View.VISIBLE);
                tv.setVisibility(View.VISIBLE);
                mLoading.setVisibility(View.GONE);
                setVisibility(VISIBLE);
                break;
            case NETWORK_LOADING:
                mErrorState = NETWORK_LOADING;
                mLoading.setVisibility(View.VISIBLE);
                retryBtn.setVisibility(GONE);
                img.setVisibility(View.GONE);
                tv.setVisibility(View.GONE);
                setVisibility(VISIBLE);
                break;
            case NODATA:
                mErrorState = NODATA;
                img.setImageResource(R.mipmap.ic_nodata);
                setErrorMessage(strNoDataContent);
                tv.setVisibility(View.VISIBLE);
                img.setVisibility(View.VISIBLE);
                mLoading.setVisibility(View.GONE);
                retryBtn.setVisibility(GONE);
                setVisibility(VISIBLE);
                break;
            case LOAD_FAILD:
                mErrorState = LOAD_FAILD;
                img.setImageResource(R.mipmap.load_faild);
                retryBtn.setText(R.string.reload);
                tv.setText(R.string.net_error);

                img.setVisibility(View.VISIBLE);
                tv.setVisibility(View.VISIBLE);
                retryBtn.setVisibility(VISIBLE);
                mLoading.setVisibility(View.GONE);
                setVisibility(VISIBLE);
                break;
            case LOAD_SUCCESS:
                mErrorState = LOAD_SUCCESS;
                break;
            default:
                setVisibility(GONE);
                break;
        }


    }

    String strNoDataContent;

    public void setNoDataContent(String noDataContent) {
        strNoDataContent = noDataContent;
    }

    RetryButtonClick retryButtonClick;

    public void setRetryButtonClick(RetryButtonClick retryButtonClick) {
        this.retryButtonClick = retryButtonClick;
    }

    public interface RetryButtonClick {
        void onClickFromRetry();
    }
}
